<?php


require_once("overhead.php");

class Section implements DataObject{

	var $ID; //corresponds to the Primary key in the Section table (also called ID)
	var $code; //coresponds to the Section code (for example, for "CS 1050A" the code is "A")
	var $professor;
	var $term;
	var $course;
	var $percent_A;
	var $percent_B;
	var $percent_C;
	var $percent_D;
	var $percent_F;
	var $percent_W;
	var $size;
	var $gpa;
	var $crn;
	var $survey;

	
	
	
	//Getters and setters don't directly touch the database.
	public function getID() {
		return $this->ID;
	}
	public function getCRN() {
		return $this->crn;
	}
	public function getProfessor() {
		return $this->professor;
	}
	public function getTerm() {
		return $this->term;
	}
	public function getCourse() {
		return $this->course;
	}
	public function getPercent_A() {
		return $this->percent_A;
	}
	public function getPercent_B() {
		return $this->percent_B;
	}
	public function getPercent_C() {
		return $this->percent_C;
	}
	public function getPercent_D() {
		return $this->percent_D;
	}
	public function getPercent_F() {
		return $this->percent_F;
	}
	public function getPercent_W() {
		return $this->percent_W;
	}
	public function getSize() {
		return $this->size;
	}
	public function getGPA() {
		return $this->gpa;
	}
	public function getCode() {
		return $this->code;
	}
	public function getSurvey() {
		return $this->survey;
	}
	public function setProfessor($p) {
		$this->professor=$p;
	}
	public function setCode($c) { 
		$this->code=$c;
	}
	public function setTerm($t) {
		$this->term = $t;
	}
	public function setCourse($c) {
		$this->course = $c;
	}
	public function setPercent_A($a) {
		$this->percent_A=$a;
	}
	public function setPercent_B($b) {
		$this->percent_B=$b;
	}
	public function setPercent_C($c) {
		$this->percent_C=$c;
	}
	public function setPercent_D($d) {
		$this->percent_D=$d;
	}	
	public function setPercent_F($f) {
		$this->percent_F=$f;
	}
	public function setPercent_W($w) {
		$this->percent_W=$w;
	}		
	public function setSize($s) {
		$this->size = $s;
	}
	public function setGPA($g) {
		$this->gpa=$g;
	}
	public function setCRN($c) {
		$this->crn=$c;
	}
	public function setSurvey($s) {
		/*if ($s!=1) $this->survey=0;
		else $this->survey=$s;*/ $this->survey = $s;
		}
	public function getSizeDesc() {
		$q = mysql_query("select Description, MinimumSize,MaximumSize from SectionSize where ID=".$this->size);
		$row=mysql_fetch_assoc($q);
		return substr($row['Description'],0,strpos($row['Description'],"("))."(".($row['MinimumSize']==0 ? 1 : $row['MinimumSize']).($row['MaximumSize']==999 ? '+' : '-'.$row['MaximumSize']).")";
	}
	public function getSizeRange() {
		$q = mysql_query("select Description, MinimumSize,MaximumSize from SectionSize where ID=".$this->size);
		$row=mysql_fetch_assoc($q);
		return ($row['MinimumSize']==0 ? 1 : $row['MinimumSize']).($row['MaximumSize']==999 ? '+' : '-'.$row['MaximumSize']);
	}
		
	public function retrieveFromID($i) { //Get Section info from the database for a Section with Section.ID=$i
		$queryString="select ID,ProfessorID, Code, CourseID, TermID, Percent_A, Percent_B, Percent_C, Percent_D, Percent_F, Percent_W, Size, GPA, CRN,Survey from Section where Section.ID=".$i;
		
		
		//print_r($queryString);
		$query = mysql_query($queryString);
		
		//print_r($query);
		
		$result=mysql_fetch_assoc($query);
		 if ($result) {
		 	//If something was found, put all the fields from the database (in $result right now) into
		 	//the instance variables for this object.
			$prof = new Professor();
 			$prof->retrieveFromID($result['ProfessorID']);
 			$c=new Course();
 			$c->retrieveFromID($result['CourseID']);
 			$t=new Term();
 			$t->retrieveFromID($result['TermID']);
 			$this->ID = $result['ID'];
 			$this->code=$result['Code'];
 			$this->professor=$prof;
 			$this->percent_A=$result['Percent_A'];
 			$this->percent_B=$result['Percent_B'];
 			$this->percent_C=$result['Percent_C'];
 			$this->percent_D=$result['Percent_D'];
 			$this->percent_F=$result['Percent_F'];
 			$this->percent_W=$result['Percent_W'];
 			$this->size=$result['Size'];
 			$this->gpa=$result['GPA'];
 			$this->crn=$result['CRN'];
 			$this->term=$t;
 			$this->course=$c;
 			$this->survey=$result["Survey"];
 			//return true because we found it!
 			return true;
 		} else {
 			//it wasn't found or there was an error, so return false.
 			return false;
 		}
	}
	
	public function update() { //Update the contents of the database to reflect any changes made to this object.
		$p = $this->professor;
		$c = $this->course;
		$t = $this->term;
		$queryString="update Section set Code='".$this->code."', ProfessorID=".$p->getID().", CourseID=".$c->getID().", TermID=".$t->getID().",Percent_A=".$this->percent_A.",Percent_B=".$this->percent_B.",Percent_C=".$this->percent_C.",Percent_D=".$this->percent_D.", Percent_F=".$this->percent_F.",Percent_W=".$this->percent_W.", GPA=".$this->gpa.", Size=".$this->size.", Survey=".$this->survey." where Section.ID=".$this->ID;
		
		//print_r($queryString);
		
		if (mysql_query($queryString)) { //if the query fails, then $query will be NULL or false. since we are not
										 //doing anything with any resulting rows, we don't have to create another variable
										 //for the result of mysql_query().
			return true; //Successfully updated!
		} else {
			return false; //There was a problem with the UPDATE query!
		}
	}
	
	public function insert() { //This takes all the values of the instance variables and creates a NEW entry in the 
							   //Section table for these values.
		$p=$this->professor;
		$c=$this->course;
		$t=$this->term;
		$queryString = "insert into Section (ID, Code, ProfessorID, CourseID, TermID, Percent_A, Percent_B, Percent_C, " . 
        "Percent_D, Percent_F, Percent_W, Size, GPA, CRN, Survey) values " . 
        "('" . $this->ID . "', '" . $this->code . "', " . $p->getID() . ", " . $c->getID() . ", " . $t->getID() . 
        ", " . $this->percent_A . ", " . $this->percent_B . ", " . $this->percent_C . ", " . $this->percent_D . 
        ", " . $this->percent_F . ", " . $this->percent_W . ", " . /*$this->size .*/ "51, " . $this->gpa . ", " . 
        $this->crn . ", " . $this->survey . ")";
		
		//print_r($queryString);
		
		if (mysql_query($queryString)) { //if the insert was successful, this will not be null or false.
			//BUT! we have one more step. if you check the spefications, it says we have to update the value
			//of $this->ID to hold the ID that the database created (with auto_increment) for this new Section.
			//We can do that by selecting LAST_INSERT_ID().
			$getIDQueryString="select LAST_INSERT_ID() as NewID ";
			$getIDQuery=mysql_query($getIDQueryString);
			$result=mysql_fetch_assoc($getIDQuery);
			if ($result) {
				$this->ID=$result['NewID'];
				return true;
			} else { echo "1"; return false; }
		} else {
      die(mysql_error());
      
			return false;
		}
	}
	
	public function delete() { //This deletes the row of the Section table that has and ID corresponding
							   //to $this->ID
		$queryString="delete from Section where Section.ID=".$this->ID;
		if (mysql_query($queryString)) {
				return true;
		}
		else {
				return false;
		}
		
	}
	public function getLatest() {
                $queryString="select  ID,ProfessorID, Code, CourseID, TermID, Percent_A, Percent_B, Percent_C, Percent_D, Percent_F, Percent_W, Size, GPA,CRN,Survey from Section where Section.ID=".$this->ID;
                $query=mysql_query($queryString);
                $result=mysql_fetch_assoc($query);
                 if ($result) {
                        //If something was found, put all the fields from the database (in $result right now) into
                        //the instance variables for this object.
						$prof = new Professor();
 						$prof->retrieveFromID($result['ProfessorID']);
						$c=new Course();
						$c->retrieveFromID($result['CourseID']);
						$t=new Term();
						$t->retrieveFromID($result['TermID']);
						$this->ID=$result['ID'];
						$this->code=$result['Code'];
						$this->professor=$prof;
						$this->percent_A=$result['Percent_A'];
						$this->percent_B=$result['Percent_B'];
						$this->percent_C=$result['Percent_C'];
						$this->percent_D=$result['Percent_D'];
						$this->percent_F=$result['Percent_F'];
						$this->size=$result['Size'];
						$this->gpa=$result['GPA'];
						$this->term=$t;
						$this->course=$c;
						$this->crn=$result['CRN'];
						$this->survey=$result['Survey'];
                        //return true because we found it!
                        return true;
                } else {
                        //it wasn't found or there was an error, so return false.
                        return false;
                }
        }
		
}

?>
